﻿using OOAD_PROJEKAT;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DAL;

namespace WpfApplication25
{
    public partial class prijaviGresku : Window
    {
        Klijent kop;
        public prijaviGresku(Klijent zap)
        {
            InitializeComponent();
            kop = zap;
            kor.BorderBrush = SystemColors.ControlDarkBrush;
            lis.BorderBrush = SystemColors.ControlDarkBrush;
            dat.BorderBrush = SystemColors.ControlDarkBrush;
            dat.SelectedDate = DateTime.Now;
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox probni = sender as CheckBox;
            foreach (CheckBox sarma in lis.Items)
            {
                if (sarma.Content != probni.Content)
                    sarma.IsChecked = false;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(kor.Text))
            {
                kor.BorderBrush = Brushes.Red;
                kor.ToolTip = "Morate unijeti upis!";
            }
            else
            {
                kor.BorderBrush = SystemColors.ControlDarkBrush;
                kor.ToolTip = null;
                if (dat.SelectedDate > DateTime.Now)
                {
                    dat.BorderBrush = Brushes.Red;
                    dat.ToolTip = "Neispravan unos datuma!";
                }
                else
                {
                    dat.BorderBrush = SystemColors.ControlDarkBrush;
                    dat.ToolTip = null;
                    bool broji = false;
                    foreach (CheckBox sam in lis.Items)
                    {
                        if (sam.IsChecked == true)
                        {
                            broji = true;
                            break;
                        }
                    }
                    if (broji == false)
                    {
                        lis.BorderBrush = Brushes.Red;
                        lis.ToolTip = "Morate odabrati vozilo!";
                    }
                    else
                    {
                        lis.BorderBrush = SystemColors.ControlDarkBrush;
                        lis.ToolTip = null;
                        DAL.DAL.AutomobilDAO auto = new DAL.DAL.AutomobilDAO();
                        List<Automobil> auta = auto.getAll();
                        string test = "";
                        foreach (CheckBox xam in lis.Items)
                        {
                            if (xam.IsChecked == true)
                            {
                                test = xam.Content.ToString();
                                break;
                            }
                        }
                        int indeks = 0;
                        for (int i = 0; i < auta.Count; i++)
                        {
                            if (auta[i].IdKlijenta == kop.Id && auta[i].RegistarskeOznake == test)
                            {
                                indeks = auta[i].Id;
                            }
                        }
                        try
                        {
                            MessageBox.Show(indeks.ToString());
                            DAL.DAL.StetaDAO xa = new DAL.DAL.StetaDAO();
                            xa.create(new Steta(kor.Text, indeks, 0, dat.SelectedDate.Value));
                            MessageBox.Show("Uspješno ste prijavili štetu!", "Šteta prijavljena!", MessageBoxButton.OK, MessageBoxImage.Information);
                        }
                        catch (Exception em)
                        {
                            MessageBox.Show(em.Message);
                        }
                       }
                }
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DAL.DAL.AutomobilDAO auto = new DAL.DAL.AutomobilDAO();
            List<Automobil> auta = auto.getAll();
            foreach (Automobil a in auta)
            {
                if (a.IdKlijenta == kop.Id)
                {
                    CheckBox x = new CheckBox();
                    x.Checked += CheckBox_Checked;
                    x.Content = a.RegistarskeOznake;
                    x.ToolTip = "Proizvodjac : " + a.Proizvodjac + ", Tip: " + a.Tip + ", godina proizvodnje: " + a.GodinaProizvodnje.ToString();
                    lis.Items.Add(x);
                }
            }
        }
    }
}
